rm(list = setdiff(ls(), lsf.str()))
load("Study 1/Altered Data/Study1_NL_12.RData")

#SI A.5: Factor loadings------------
latent<-(with(data, data.frame(agre1, agre2, agre3, agre4, agre5, agre6, agre7, agre8, agre9, agre10, open1 , open2, open3, open4, open5, open6, open7, open8, open9, open10, con1, con2, con3, con4, con5, con6, con7, con8, con9, con10, ext1, ext2, ext3, ext4, ext5,ext6,ext7,ext8,ext9,ext10, neu1,neu2,neu3,neu4,neu5,neu6,neu7,neu8,neu9,neu10)))
latent <- na.omit(latent)

CFA_agree <-'Agree = ~ NA*agre1+agre2+agre3+agre4+agre5+agre6+agre7+agre8+agre9+agre10 
Open = ~ NA*open1 + open2 + open3 + open4 + open5+open6+open7+open8+open9+open10
Con = ~ NA*con1+con2+con3+con4+con5+con6+con7+con8+con9+con10
Ext = ~ NA*ext1+ext2+ext3+ext4+ext5+ext6+ext7+ext8+ext9+ext10
Neu = ~ NA*neu1+neu2+neu3+neu4+neu5+neu6+neu7+neu8+neu9+neu10
Agree ~~ 1*Agree
Open ~~ 1*Open
Con ~~ 1*Con
Ext ~~ 1*Ext
Neu ~~ 1*Neu
' 

fit<-cfa(CFA_agree, data=latent)
p<-parameterEstimates(fit, standardized=TRUE) %>%  dplyr::select(std.all, pvalue)
p <- p[1:50, ] 
names(p) <- c("Standardized Factor Loading", "p-value")
(setattr(p, "row.names", c("A1: Am interested in people", "A2: Sympathize with others feelings", "A3: Have a soft heart", "A4: Take time out for others", "A5: Feel others emotions", "A6: Make people feel at ease", "A7: Feel little concern for others", "A8: Insult people", "A9: Am not interested in other peoples problems", "A10: Am not really interested in others", "O1: Have a rich vocabulary", "O2: Have a vivid imagination", "O3: Have excellent ideas", "O4: Am quick to understand things", "O5: Use difficult words", "O6: Spend time reflecting on things", "O7: Am full of ideas", "O8: Have difficulty understanding abstract ideas", "O9: Am not interested in abstract ideas", "O10: Do not have a good imagination", "C1: Am always prepared", "C2: Pay attention to details", "C3: Get chores done right away", "C4: Like order", "C5: Follow a schedule", "C6: Am exacting in my work", "C7: Leave my belongings around", "C8: Make a mess of things", "C9: Often forget to put things back in their proper place", "C10: Shirk my duties",  "E1: Am the life of the party", "E2: Feel comfortable around people", "E3: Start conversations", "E4: Talk to a lot of different people at partie", "E5: Do not mind being the center of attention", "E6: Do not talk a lot", "E7: Keep in the background", "E8: Have little to say", "E9: Do no like to draw attention to myself", "E10: Am quiet around strangers", "N1: Get stressed out easily", "N2: Worry about things", "N3: Am easily disturbed", "N4: Get upset easily", "N5: Change my mood a lot", "N6: Have frequent mood swings", "N7: Get irritated easily", "N8: Often feel blue", "N9: Am relaxed most of the time", "N10: Seldom feel blue")))

p<-xtable(caption = "Dutch Election Study 2012: Big Five Standardized Factor Loadings", label = "tab:NL12_cfa", p)
print(p, type="latex", file="Tables/NL_factor.tex", size="tiny", caption.placement="top")

#SI A.5: Correlation matrix----------------------
myvars <- c("agre", "open", "con", "ext", "neu", "cynicism11" , "anti_immi11" , "econ_cons11")
cor_ivs <- data[myvars]
names(cor_ivs) <-  c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                     "4. Extraversion", "5. Neuroticism", "6.Cynicism",
                     "7. Social conservatism", "8. Economic conservatism")

correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:7)]
names(correlation.matrix) <- seq(1,7,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="Dutch Election Study: Correlation Matrix of Independent Variables", out="Tables/NL_cor.tex", no.space=TRUE, label="tab:NL_cor", digits=2)

#SI A.5: Results belonging to Figure 1---------------------------
vote.pvv<-list()
vote.pvv[[1]]<-glm(populist ~  zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age , data=data, family="binomial")
vote.pvv[[2]]<-glm(populist ~  zero1(agre) + zero1(open) + zero1(con) + zero1(ext) + zero1(neu) + female + age + edu2 + edu3 + edu4 + edu5 + edu6 +income + income_missing + zero1(econ_cons11) + zero1(anti_immi11) + zero1(cynicism11), data=data, family="binomial")

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age",  "Education (Ref: primary): Intermediate secondary education", "Higher secondary education", "Intermediate vocational education", "Higher vocational education", "University", "Income", "Income missing", "Economic conservatism","Social conservatism",  "Political cynicism")
stargazer2(vote.pvv, odd.ratio = TRUE, title="Dutch Election Study 2012: Support for the PVV", align=TRUE,  omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.1, 0.05),  covariate.labels = labels,dep.var.labels.include = FALSE, model.numbers= FALSE, font.size = "tiny",column.labels = c("Base", "Figure 1"),  notes = "Odss ratios with standard errors from logistic regression models; *p<0.1, **p<0.05",  notes.append = FALSE, out="Tables/NL12_results.tex", no.space=TRUE, label="tab:NL12_results" )

#calculate 1 SD above and below the mean
low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

odds.difference <- (exp(vote.pvv[[2]]$coefficients)[2]*low) / (exp(vote.pvv[[2]]$coefficients)[2]*high)
1/odds.difference

#SI A.5: Descriptives----------------
desc.labels <- c("Populist Vote",labels)
stargazer(vote.pvv[[2]]$model, type = "latex", covariate.labels=desc.labels, summary.stat = c("mean", "sd", "N", "median","min",  "max"), title="Descriptive statistics Dutch Election Study 2012", out="Tables/NL_descrip.tex", no.space=TRUE, label="tab:NL_descriptives", digits=2)

#correlation between ideological dimensions
cor.test(data$econ_cons11, data$anti_immi11)
#alpha Openness
open<-data.frame(data$open1, data$open2, data$open3, data$open4, data$open5, data$open6, data$open7, data$open8, data$open9, data$open10)
psych::alpha(open)
#alpha Conscientiouness
con_a<-data.frame(data$con1, data$con2, data$con3, data$con4, data$con5, data$con6, data$con7, data$con8, data$con9, data$con10)
psych::alpha(con_a)
#alpha Extraversion
ext_a<-data.frame(data$ext1, data$ext2, data$ext3, data$ext4, data$ext5, data$ext6, data$ext7, data$ext8, data$ext9, data$ext10)
psych::alpha(ext_a)
#alpha Agreeableness
agre_a<-(data.frame(data$agre1, data$agre2, data$agre3, data$agre4, data$agre5, data$ext6, data$agre7, data$agre8, data$agre9, data$agre10))
psych::alpha(agre_a)
#alpha Neuroticism
neu_a<-data.frame(data$neu1, data$neu2, data$neu3, data$neu4, data$neu5, data$neu6, data$neu7, data$neu8, data$neu9, data$neu10)
psych::alpha(neu_a)
#alpha cynicism
cyn_a<-data.frame(data$cyn1_11,data$cyn2_11,data$cyn3_11)
psych::alpha(cyn_a)

#median income
median(as.numeric(data$nettocat)[as.numeric(data$nettocat)<12], na.rm=T)

